package com.leapp.sk.ui.activity.classs

import android.content.Intent
import android.os.Bundle
import android.view.View
import cc.vv.lkbasecomponent.http.LKHttp
import cc.vv.lkbasecomponent.http.lib.setting.Settings
import cc.vv.lklibrary.http.base.okhttpfactory.plugins.httpplugin.MediaTypeWrap
import com.example.baselibrary.db.dao.DNDDao
import com.example.baselibrary.db.table.DNDTable
import com.example.baselibrary.util.LKToastUtil
import com.example.mvp.activity.BaseActivityMVP
import com.example.mvp.binder.BaseDataBinder
import com.leapp.sk.R
import com.leapp.sk.app.CLASS_DETIAL
import com.leapp.sk.app.CLASS_DETIAL_MODIFY
import com.leapp.sk.bean.request.classs.ClassDetialRequestEntity
import com.leapp.sk.bean.request.classs.ModifyClassNameRequestEntity
import com.leapp.sk.bean.response.BaseResponseDataEntity
import com.leapp.sk.bean.response.classs.ClassDetialResponseEntity
import com.leapp.sk.binder.BaseBinder
import com.leapp.sk.sever.classs.ClassDetialSever
import com.example.util.finallist.*
import com.leapp.sk.viewdelegate.classs.ClassDetialDelegate

/**
 * 班级详情
 * @author  chaohaipeng  on 2018/7/30.
 */
class ClassDetialActivity:BaseActivityMVP<ClassDetialDelegate>(){

    /**
     * 群ID
     */
    var conversationId:String?=""

    /**
     * 开关是否打开
     */
      var isChecked:Boolean=false
    /**
     * 班级和宝贝关系ID
     */
    var classPersonnelId:String?=""
    override fun bindEvenListener() {
//       viewDelegate.setOnClickListener(View.OnClickListener {
//           val intent: Intent =Intent(this,ModifyClassNameASctivity::class.java)
//           intent.putExtra(CLASS_NICK,viewDelegate.getclassNameContent())
//           startActivityForResult(intent,CODE_201)
//       },R.id.rl_acd_class_name)

        viewDelegate.setOnClickListener(View.OnClickListener {

            val intent: Intent =Intent(this,ModifyGroupNameActiity::class.java)
            intent.putExtra(MY_GROUP_NICK,viewDelegate.getNickContent())
            intent.putExtra(CLASS_PERSONNELID,classPersonnelId)
            startActivityForResult(intent,CODE_206)
        },R.id.rl_acd_group_nick)
        viewDelegate.setOnClickListener(View.OnClickListener {
            //开关状态 isChecked
            if (isChecked){
                viewDelegate.setPushbtn(false)
                isChecked=false
                httpnoDisturbModify(1)
            }else{
                httpnoDisturbModify(0)
                viewDelegate.setPushbtn(true)
                isChecked=true
            }
        },R.id.iv_acd_message_dustrib)
        viewDelegate.setOnClickListener(View.OnClickListener {
            val intent: Intent =Intent(this,ClassMemberActivity::class.java)
          startActivity(intent)
        },R.id.ll_acd_member_num)
    }
    override fun getDelegateClass(): Class<ClassDetialDelegate> {
        return ClassDetialDelegate::class.java
    }
    override fun initView(p0: Bundle?) {
        viewDelegate.setTitle(resources.getString(R.string.string_class_detial))
    }
    override fun initDate() {
         conversationId = intent?.getStringExtra(MESSAGE_LIST_ID)
        val queryById = DNDDao.getInstance().queryById(conversationId)
        if (queryById!=null){
            isChecked=true
        }
        httpClassDetial()

    }

    override fun getDataBinder(): BaseDataBinder<*, *> {
        return BaseBinder()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when(requestCode){
            CODE_206->{
                if(requestCode==CODE_206){
                    val stringExtra = data?.getStringExtra(MY_GROUP_NICK)
                    viewDelegate?.setNickContent(stringExtra)
                }
            }
            CODE_201->{
                if(requestCode==CODE_201){
                    val stringExtra = data?.getStringExtra(CLASS_NICK)
                    viewDelegate?.setclassNameContent(stringExtra)
                }
            }
        }
    }
    /**
     * 班级修改消息免打扰
     */
    private fun httpnoDisturbModify(noDisturb:Int) {
        if (classPersonnelId==null){
            return
        }
        val modifyClassNameRequestEntity = ModifyClassNameRequestEntity()
        modifyClassNameRequestEntity.id=classPersonnelId!!
        modifyClassNameRequestEntity.noDisturb=""+noDisturb
        LKHttp.post(CLASS_DETIAL_MODIFY, modifyClassNameRequestEntity, BaseResponseDataEntity::class.java, object : BaseCallBack<BaseResponseDataEntity>(this) {
            override fun onSuccess(url: String?, resultText: String?, obj: BaseResponseDataEntity?) {
                super.onSuccess(url, resultText, obj)
                if (obj?.code == SKCODE_200) {
                    if (noDisturb==0){
                        DNDDao.getInstance().insert(DNDTable(conversationId))
                    }else{
                        DNDDao.getInstance().deleteById(conversationId)
                    }
                }else{
                    LKToastUtil.showToastShort(obj?.message)
                }
            }
            override fun onFailure(url: String?, isLoading: Boolean, exceptionStr: String?) {
                super.onFailure(url, isLoading, exceptionStr)
                LKToastUtil.showToastShort(resources.getString(R.string.app_net_erro))
            }
        }
                , true, Settings(MediaTypeWrap.MEDIA_TYPE_FORM))

    }


    /**
     * 班级详情
     */
    private fun httpClassDetial() {
        val classDetialRequestEntity = ClassDetialRequestEntity()
        classDetialRequestEntity.id=classDetialRequestEntity.userId
        classDetialRequestEntity.parentNum=5
        LKHttp.post(CLASS_DETIAL, classDetialRequestEntity, ClassDetialResponseEntity::class.java, object : BaseCallBack<ClassDetialResponseEntity>(this) {
            override fun onSuccess(url: String?, resultText: String?, obj: ClassDetialResponseEntity?) {
                super.onSuccess(url, resultText, obj)
                if (obj?.code == SKCODE_200) {
                    ClassDetialSever.getInstance().binderData(this@ClassDetialActivity,viewDelegate,obj)
                    classPersonnelId= ClassDetialSever.getInstance().getClassPersonnelId(obj)
                }else{
                    LKToastUtil.showToastShort(obj?.message)
                }
            }
            override fun onFailure(url: String?, isLoading: Boolean, exceptionStr: String?) {
                super.onFailure(url, isLoading, exceptionStr)
                LKToastUtil.showToastShort(resources.getString(R.string.app_net_erro))
            }
        }
                , true, Settings(MediaTypeWrap.MEDIA_TYPE_FORM))

    }

}